﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSem3.Admin
{
    public partial class WebForm13 : System.Web.UI.Page
    {
        dataDataContext dc = new dataDataContext();
               
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
                loaddata();
                
                
            }
        }

        protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
            Label lbl = ListView1.Items[e.NewSelectedIndex].FindControl("lblOrderID") as Label;
            Response.Redirect("orderDetail.aspx?id=" + Convert.ToInt32(lbl.Text));
        }

        protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
          
            Label orderid = ListView1.Items[e.ItemIndex].FindControl("lblOrderID") as Label;
            CheckBox chk= ListView1.Items[e.ItemIndex].FindControl("chkOrderStatus") as CheckBox;
            DropDownList empID = ListView1.Items[e.ItemIndex].FindControl("ddlEmp") as DropDownList;
            try{
                Order ord = dc.Orders.Single(o=>o.OrderID==Convert.ToInt64(orderid.Text));
                ord.Status = chk.Checked;
                ord.EmployeeID = Convert.ToInt32(empID.SelectedValue.ToString());
                dc.SubmitChanges();
                loaddata();
            }
            catch(Exception){
               
            }
        }

        protected void ListView1_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
        {

        }


        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
           
            Label lbl = e.Item.FindControl("lblOrderID") as Label;
            DropDownList ddl= e.Item.FindControl("ddlEmp") as DropDownList;
            ddl.DataSource = dc.Employees.ToList();
            ddl.DataTextField = "username";
            ddl.DataValueField = "employeeid";
            ddl.SelectedIndex = -1;
            ddl.DataBind();

        }
        private void loaddata()
        {
            var data = dc.Orders.OrderByDescending(d => d.DateOrder).ToList();
            ListView1.DataSource = data;
            ListView1.DataBind();
        }
    }
}